//  https://www.luogu.com.cn/problem/P2858

#include<iostream>
using namespace std;

const int N = 2010, M = 1010;
int dp[N][N];
int v[M];

int main()
{
    int n;cin >> n;
    for (int i = 1;i <= n;i++) cin >> v[i];

    for (int len = 1;len <= n;len++)
        for (int i = 1;i + len - 1 <= n;i++)
        {
            int j = i + len - 1;
            dp[i][j] = max(dp[i + 1][j] + (n - len + 1) * v[i], dp[i][j - 1] + (n - len + 1) * v[j]);
        }

    cout << dp[1][n];
    return 0;
}